<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of v_rotro2qr</title>
  <meta name="keywords" content="v_rotro2qr">
  <meta name="description" content="V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../index.html">Home</a> &gt;  <a href="index.html">v_mfiles</a> &gt; v_rotro2qr.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for v_mfiles&nbsp;<img alt=">" border="0" src="../right.png"></a></td></tr></table>-->

<h1>v_rotro2qr
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="box"><strong>function q=v_rotro2qr(r) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre class="comment">V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion
 Inputs:

     R(3,3,...)   Input rotation matrix

 Outputs:

     Q(4,...)   normalized real-valued quaternion

 In the quaternion representation of a rotation, and q(1) = cos(t/2)
 where t is the angle of rotation in the range 0 to 2pi
 and q(2:4)/sin(t/2) is a unit vector lying along the axis of rotation
 a positive rotation about [0 0 1] takes the X axis towards the Y axis.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_rotqr2ro.html" class="code" title="function r=v_rotqr2ro(q)">v_rotqr2ro</a>	ROTQR2RO converts a real quaternion to a 3x3 rotation matrix</li></ul>
This function is called by:
<ul style="list-style-image:url(../matlabicon.gif)">
<li><a href="v_rectifyhomog.html" class="code" title="function [imr,xa,ya]=v_rectifyhomog(ims,roc,k0,mode)">v_rectifyhomog</a>	V_RECTIFYHOMOG Apply rectifying homographies to an image set</li><li><a href="v_rotpl2ro.html" class="code" title="function r=v_rotpl2ro(u,v,t)">v_rotpl2ro</a>	V_ROTPL2RO find matrix to rotate in the plane containing u and v r=[u,v,t]</li><li><a href="v_rotro2eu.html" class="code" title="function e=v_rotro2eu(m,r)">v_rotro2eu</a>	V_ROTRO2EU converts a 3x3 rotation matrix into the corresponding euler angles</li><li><a href="v_rotro2pl.html" class="code" title="function [u,v,t]=v_rotro2pl(r)">v_rotro2pl</a>	V_ROTRO2PL find the plane and rotation angle of a rotation matrix [u,v,t]=r</li></ul>
<!-- crossreference -->


<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function q=v_rotro2qr(r)</a>
0002 <span class="comment">%V_ROTRO2QR converts a 3x3 rotation matrix to a real quaternion</span>
0003 <span class="comment">% Inputs:</span>
0004 <span class="comment">%</span>
0005 <span class="comment">%     R(3,3,...)   Input rotation matrix</span>
0006 <span class="comment">%</span>
0007 <span class="comment">% Outputs:</span>
0008 <span class="comment">%</span>
0009 <span class="comment">%     Q(4,...)   normalized real-valued quaternion</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% In the quaternion representation of a rotation, and q(1) = cos(t/2)</span>
0012 <span class="comment">% where t is the angle of rotation in the range 0 to 2pi</span>
0013 <span class="comment">% and q(2:4)/sin(t/2) is a unit vector lying along the axis of rotation</span>
0014 <span class="comment">% a positive rotation about [0 0 1] takes the X axis towards the Y axis.</span>
0015 
0016 <span class="comment">%      Copyright (C) Mike Brookes 2007-2018</span>
0017 <span class="comment">%      Version: $Id: v_rotro2qr.m 10865 2018-09-21 17:22:45Z dmb $</span>
0018 <span class="comment">%</span>
0019 <span class="comment">%   VOICEBOX is a MATLAB toolbox for speech processing.</span>
0020 <span class="comment">%   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html</span>
0021 <span class="comment">%</span>
0022 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0023 <span class="comment">%   This program is free software; you can redistribute it and/or modify</span>
0024 <span class="comment">%   it under the terms of the GNU General Public License as published by</span>
0025 <span class="comment">%   the Free Software Foundation; either version 2 of the License, or</span>
0026 <span class="comment">%   (at your option) any later version.</span>
0027 <span class="comment">%</span>
0028 <span class="comment">%   This program is distributed in the hope that it will be useful,</span>
0029 <span class="comment">%   but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
0030 <span class="comment">%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
0031 <span class="comment">%   GNU General Public License for more details.</span>
0032 <span class="comment">%</span>
0033 <span class="comment">%   You can obtain a copy of the GNU General Public License from</span>
0034 <span class="comment">%   http://www.gnu.org/copyleft/gpl.html or by writing to</span>
0035 <span class="comment">%   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.</span>
0036 <span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
0037 
0038 <span class="comment">% in the comments below, t is the rotation angle, a is the rotation axis</span>
0039 sz=size(r);
0040 r=reshape(r,9,[]); <span class="comment">% make 2-dimensional</span>
0041 q=zeros(4,size(r,2));
0042 d = 1+r(1,:)+r(5,:)+r(9,:);     <span class="comment">% 2(1+cos(t)) = 4(cos(t/2))^2 = 4 q(1)^2</span>
0043 mm=d&gt;1; <span class="comment">% mask for rotation angles &lt; 120 degrees</span>
0044 <span class="keyword">if</span> any(mm)                       <span class="comment">% for rotation angles less than 120 degrees</span>
0045     s = sqrt(d(mm))*2;            <span class="comment">% 4 cos(t/2) = 2 sin(t)/sin(t/2)</span>
0046     q(2,mm) = (r(6,mm)-r(8,mm))./s;
0047     q(3,mm) = (r(7,mm)-r(3,mm))./s;
0048     q(4,mm) = (r(2,mm)-r(4,mm))./s;
0049     q(1,mm) = 0.25*s;            <span class="comment">% cos(t/2)</span>
0050 <span class="keyword">end</span>
0051 <span class="keyword">if</span> any(~mm)
0052     m=(r(1,:)&gt;r(5,:)) &amp; (r(1,:)&gt;r(9,:)) &amp; ~mm;
0053     <span class="keyword">if</span> any(m)
0054         s  = sqrt( 1.0+r(1,m)-r(5,m)-r(9,m))*2;  <span class="comment">% s&gt;=2 always: s=4 a(1) sin(t/2) = 2 a(2) (1-cos(t))/sin(t/2)</span>
0055         q(2,m) = 0.25*s;
0056         q(3,m) = (r(2,m)+r(4,m))./s;
0057         q(4,m) = (r(7,m)+r(3,m))./s;
0058         q(1,m) = (r(6,m)-r(8,m))./s;
0059         mm=mm|m;
0060     <span class="keyword">end</span>
0061     m=(r(5,:)&gt;r(9,:)) &amp; ~mm;
0062     <span class="keyword">if</span> any(m)
0063         s  = sqrt( 1.0+r(5,m)-r(1,m)-r(9,m))*2; <span class="comment">% s&gt;=2 always: s=4 a(2) sin(t/2)</span>
0064         q(2,m) = (r(2,m)+r(4,m))./s;
0065         q(3,m) = 0.25*s;
0066         q(4,m) = (r(6,m)+r(8,m))./s;
0067         q(1,m) = (r(7,m)-r(3,m))./s;
0068         mm=mm|m;
0069     <span class="keyword">end</span>
0070     <span class="keyword">if</span> any(~mm)
0071         m=~mm;
0072         s  = sqrt( 1.0+r(9,m)-r(1,m)-r(5,m))*2;  <span class="comment">% s&gt;=2 always: s=4 a(3) sin(t/2)</span>
0073         q(2,m) = (r(7,m)+r(3,m))./s;
0074         q(3,m) = (r(6,m)+r(8,m))./s;
0075         q(4,m) = 0.25*s;
0076         q(1,m) = (r(2,m)-r(4,m))./s;
0077     <span class="keyword">end</span>
0078 <span class="keyword">end</span>
0079 <span class="keyword">if</span> max(abs(sum(q.^2,1)-1))&gt;1e-8; <span class="comment">% check normalization</span>
0080     error(<span class="string">'Input to rotro2qr must be a rotation matrix with det(r)=+1'</span>);
0081 <span class="keyword">end</span>
0082 <span class="keyword">if</span> length(sz)&lt;3
0083     sz=[4 1];
0084 <span class="keyword">else</span>
0085     sz=[4 sz(3:end)];
0086 <span class="keyword">end</span>
0087 q=reshape(q.*repmat(sign([8 4 2 1]*sign(q)),4,1),sz); <span class="comment">% force leading coefficient to be positive and reshape</span>
0088 <span class="keyword">if</span> ~nargout
0089     <a href="v_rotqr2ro.html" class="code" title="function r=v_rotqr2ro(q)">v_rotqr2ro</a>(q); <span class="comment">% plot a cube</span>
0090 <span class="keyword">end</span></pre></div>
<hr><address>Generated by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>